package com.ruoyi.project.system.notice.controller;

import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.framework.aspectj.lang.annotation.DataScope;
import com.ruoyi.project.system.notice.domain.NoticeUser;
import com.ruoyi.project.system.user.domain.User;
import com.ruoyi.project.system.user.service.IUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.system.notice.domain.Notice;
import com.ruoyi.project.system.notice.service.INoticeService;

/**
 * 公告 信息操作处理
 *
 * @author ruoyi
 */
@Controller
@RequestMapping("/system/notice")
public class NoticeController extends BaseController
{
    private String prefix = "system/notice";

    @Autowired
    private INoticeService noticeService;
    @Autowired
    private IUserService userService;

    @RequiresPermissions("system:notice:view")
    @GetMapping()
    public String notice()
    {
        return prefix + "/notice";
    }

    /**
     * 查询公告列表
     */
    @RequiresPermissions("system:notice:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Notice notice)
    {
        startPage();
        List<Notice> list = noticeService.selectNoticeList(notice);
        return getDataTable(list);
    }

    /**
     * 新增公告
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存公告
     */
    @RequiresPermissions({"system:notice:add","system:batch:notice"})
    @Log(title = "通知公告", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Notice notice)
    {
        return toAjax(noticeService.insertNotice(notice));
    }

    /**
     * 修改公告
     */
    @GetMapping("/edit/{noticeId}")
    public String edit(@PathVariable("noticeId") Long noticeId, ModelMap mmap)
    {
        mmap.put("notice", noticeService.selectNoticeById(noticeId));
        return prefix + "/edit";
    }

    /**
     * 修改保存公告
     */
    @RequiresPermissions("system:notice:edit")
    @Log(title = "通知公告", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Notice notice)
    {
        return toAjax(noticeService.updateNotice(notice));
    }

    /**
     * 删除公告
     */
    @RequiresPermissions("system:notice:remove")
    @Log(title = "通知公告", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(noticeService.deleteNoticeByIds(ids));
    }

    /**
     * 跳转选人页面
     * @param notice
     * @param mmap
     * @return
     */
    @GetMapping("/user")
    public String user(Notice notice, ModelMap mmap) {
        mmap.put("notice", notice);
        User sysUser = ShiroUtils.getSysUser();
        mmap.put("deptId", sysUser.getDept().getDeptId());
        mmap.put("deptName", sysUser.getDept().getDeptName());
        return prefix + "/selectUser";
    }

    /**
     * 查询接收人
     * @param noticeUser
     * @return
     */
    @RequiresPermissions("system:notice:edit")
    @PostMapping("/user/list")
    @ResponseBody
    @DataScope(userAlias = "a", deptAlias = "b")
    public TableDataInfo userList(NoticeUser noticeUser) {
        startPage();
//        noticeUser.setDeptId(ShiroUtils.getSysUser().getDept().getDeptId());
        List<User> list = userService.noticeUserList(noticeUser);
        return getDataTable(list);
    }

    /**
     * 详情查看
     * @param noticeId
     * @param mmap
     * @return
     */
    @GetMapping("/detail/{noticeId}")
    public String detail(@PathVariable Long noticeId, ModelMap mmap) {
        Notice notice = noticeService.selectNoticeById(noticeId);
        if ("1".equals(notice.getNoticeType())) {
            NoticeUser noticeUser = new NoticeUser();
            noticeUser.setNid(noticeId);
            noticeUser.setRecipient(ShiroUtils.getLoginName());
            noticeUser.setStatus("1");
            noticeUser.setReadTime(DateUtils.getNowDate());
            noticeService.readNotice(noticeUser);
        }
        mmap.put("notice", notice);
        return prefix + "/detail";
    }

    // 进入查看列表页面
    @GetMapping("/show")
    public String show(ModelMap mmap) {
        return prefix + "/show";
    }

    // 待查看通知
    @PostMapping("/list/show")
    @ResponseBody
    public TableDataInfo listShow(Notice notice)
    {
        startPage();
        List<Notice> list = noticeService.selectNoticeList(notice);
        return getDataTable(list);
    }
}
